//
//  WelfareIncomeViewController.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2019/2/25.
//  Copyright © 2019 Gox_WallyDiao. All rights reserved.
//

import UIKit

class WelfareIncomeViewController: TTBaseVC {
    
    let WelfareListenTableViewCellID = "WelfareListenTableViewCell"
    
    lazy var incomeModel: [WelfareIncomeModel] = [{
        return WelfareIncomeModel()
        }()]
    
    private lazy var tableView: TTTableView = {
        let tableView = TTTableView.init(frame: FRAME_CGFloat(0, 57, SCREEN_WIDTH, SCREEN_HEIGHT - OFFSET_TOP - 57), style: UITableView.Style.plain)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = WhiteBackColor
        tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
        tableView.register(WelfareListenTableViewCell.self, forCellReuseIdentifier: WelfareListenTableViewCellID)
        tableView.tableFooterView = UIView.init(frame: .zero)
//        tableView.tableHeaderView = UIView.init(frame: FRAME_CGFloat(0, 0, SCREEN_WIDTH, 8))
        return tableView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "收入排行榜"
        let bc = UIImageView.init(frame: FRAME_CGFloat(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - OFFSET_TOP))
        bc.backgroundColor = RGB(255, 251, 198, 1)
        self.view.addSubview(bc)
        
        let incom = IncomeHeaderView.init(frame: FRAME_CGFloat(0, 0, SCREEN_WIDTH, 52))
        incom.dateBlock = { (num) in
            switch num {
            case 11:
                self.setData(type: "day")
            case 12:
                self.setData(type: "week")
            case 13:
                self.setData(type: "month")
            default:
                return
            }
        }
        self.view.addSubview(incom)
        
        self.view.addSubview(tableView)
        tableView.backgroundColor = UIColor.clear
        
        self.setData(type: "day")
    }
    
}

//  MARK: - ###### 方法 ######
extension WelfareIncomeViewController {
    func setData(type:String) {
        MTProgressHUD.show()
        WelfareRequestModel.loadWelfareIncomeData(para: type) { (success, message, model) in
            if success && model != nil {
                self.incomeModel = model!
                self.tableView.reloadData()
            }
            if self.incomeModel.count > 0 {
                self.tableView.setNoData(nodata: false)
            } else {
                self.tableView.setNoData(nodata: true)
            }
            MTProgressHUD.hidded(true)
        }
    }
}

//  MARK: - ###### 协议 ######
extension WelfareIncomeViewController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.incomeModel.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 53
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: WelfareListenTableViewCellID) as! WelfareListenTableViewCell
        cell.isUserInteractionEnabled = false
        cell.backgroundColor = UIColor.clear
        if indexPath.row == 0 {
            cell.rankingImage.image = IMAGE(name: "ranking1")
            cell.rankingImage.isHidden = false
            cell.rankingLabel.isHidden = true
        } else if indexPath.row == 1 {
            cell.rankingImage.image = IMAGE(name: "ranking2")
            cell.rankingImage.isHidden = false
            cell.rankingLabel.isHidden = true
        } else if indexPath.row == 2 {
            cell.rankingImage.image = IMAGE(name: "ranking3")
            cell.rankingImage.isHidden = false
            cell.rankingLabel.isHidden = true
        } else {
            cell.rankingImage.isHidden = true
            cell.rankingLabel.isHidden = false
            cell.rankingLabel.text = "\(indexPath.row + 1)"
        }
        let mol:WelfareIncomeModel = self.incomeModel[indexPath.row]
        cell.headImage.sd_setImage(with: URL(string:(StringIncludeChineseToString(mol.wxAvatarUrl ?? ""))), placeholderImage: IMAGE(name: "Mine_head"), options: .lowPriority, completed: nil)
        cell.nameLabel.text = mol.wxNickname
        cell.numberLabel.text = "\(mol.sumBrokerage)"
        cell.unitlLable.text = "听币"
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
}
